//
//  BQAdressVC.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/5/6.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQAdressVC.h"
#import "BQAdressModel.h"
#import "BQAdressCell.h"
#import "UIColor+HMAddition.h"
#import "BQShopCartViewController.h"
@interface BQAdressVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableView *adressTV;

@end

static NSString *adress_cell = @"adress_cell";

@implementation BQAdressVC{
    
    NSArray *_dataArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的收货地址";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *image =[UIImage imageNamed:@"v2_goback-1"];
    UIButton *btn =[[UIButton alloc]init];
    [btn  setBackgroundImage:image forState:UIControlStateNormal];
    btn.frame =CGRectMake(0, 0, 24, 24);
    
    UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithCustomView:btn];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =button;
    
    
    [self setupUI];
    
    [self loadData];
    
}


-(void)click{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

-(void)setupUI{
    
    UITableView *adressTV = [[UITableView alloc]init];
    
    adressTV.backgroundColor = [UIColor hm_colorWithR:239 G:239 B:239];
    
    _adressTV = adressTV;
    
    adressTV.delegate = self;
    
    adressTV.dataSource = self;
    
    [self.view addSubview:adressTV];
    
    [adressTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [adressTV registerNib:[UINib nibWithNibName:@"BQAdressCell" bundle:nil] forCellReuseIdentifier:adress_cell];
    
    adressTV.tableFooterView = [UIView new];
    
    adressTV.rowHeight = 75;
    
    UIView *addView = [[UIView alloc]init];
    
    [self.view addSubview:addView];
    
    [addView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        
        make.height.mas_offset(64);
    }];
    
    addView.backgroundColor = [UIColor whiteColor];
    
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
    
    
    
    [addView addSubview:addButton];
    
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(addView.mas_centerX);
        make.centerY.equalTo(addView.mas_centerY);
        make.height.mas_offset(50);
        make.width.mas_offset(300);
    }];
    
    [addButton setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    
    [addButton setTitle:@"+新增地址" forState:UIControlStateNormal];
    
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    addButton.titleLabel.font = [UIFont systemFontOfSize: 16];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BQAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:adress_cell forIndexPath:indexPath];
    
    
    cell.model = _dataArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}


-(void)loadData{
    
    NSDictionary *para = @{@"call" : @12};
    
    [_AccountMgr POSTWithUrlString:adress_url parameters:para success:^(id responseObject) {
        
        // 获取分类模型数组
        NSArray *dictArray = responseObject[@"data"];
        
        _dataArray = [NSArray yy_modelArrayWithClass:[BQAdressModel class] json:dictArray];
        
        
        [self.adressTV reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (_adressBlock) {
        _adressBlock(_dataArray[indexPath.row]);
    }
  
    [self.navigationController popViewControllerAnimated:YES];
}

@end
